//上传-预览逻辑

$(function($) {
	var entityID = getQueryString('id');
	if (entityID)
		$.post(contextPath + "/commission/getCommission", {"id": entityID}, function(result) {
			$("#form-add").JsonToForm(result);
		});

	getIssuePeriodSelectList();
	
	$("#form-add").validate({
		rules: {
			fullName:{
				required:true,
			},
			phone:{
				required:true,
				isMobile:true,
			},
			quantity:{
				required:true,
				isIntGtZero:true,
			},
			commission:{
				required:true,
				isIntGtZero:true,
			}
		},
		onkeyup: function(element){
			$(element).valid();
		},
		submitHandler: function(form) {
			$.ajax({
				type: "post",
				url: contextPath + "/commission/" + (entityID ? "update" : "add"),
				data: $(form).serialize(),
				dataType: "json",
				success: function(data) {
					if(data.status == 200) {
						var message = entityID ? '更新成功!' : '添加成功!';
						var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引,隐藏layer层和shade
						parent.$('#layui-layer' + index).css({'display': 'none'});
						parent.$('#layui-layer-shade' + index).css({'display': 'none'});
						parent.reloadTable(); //再刷新DT
						parent.showSuccessMessage(message, null, function() {
							parent.layer.close(index); //然后执行关闭     
						});
					} else {
						var message = entityID ? '添加成功!' : '添加失败!';
						parent.showFailMessage(message);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					alert(errorThrown);
					return false;
				}
			});
			return false;
		}
	});

});

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = location.search.substr(1).match(reg);
	if(r != null)
		return unescape(r[2]);
	return null;
}

function getIssuePeriodSelectList(){
	jQuery.ajax({
	      type:"POST",
	      dataType:"json",
	      url:contextPath + "/issuePeriod/getSelectList",
	      success:function(data){
	        var unitObj=document.getElementById("issuePeriodSelectList");
	        if(data != null){ 
	        	//alert(JSON.stringify(selectList));
	          for(var i = 0; i < data.length; i++){
	          //先创建好select里面的option元素
	            var option = document.createElement("option");
	            //转换DOM对象为JQ对象,好用JQ里面提供的方法 给option的value赋值
	            $(option).val(data[i].value);
	            //给option的text赋值,这就是你点开下拉框能够看到的东西
	            $(option).text(data[i].text);
	            //获取select 下拉框对象,并将option添加进select
	            $('#issuePeriodSelectList').append(option);
	            if (i == 0)
	            	option.selected = true;
	          }
	        }
	      },
	      error:function(){
	        J.alert('Error loading Json document');        
	      }
	    });
}
